﻿using System;
using System.Data;
using JTLeigh.Commerce.Spencer.Core.Common;
using JTLeigh.Commerce.Spencer.Core.Common.Entities;

namespace JTLeigh.Commerce.Spencer.Core.Data
{

	/// <summary>
	/// <see langword="static"/> methods for reading relations' associations and disassociations 
	/// from the database.
	/// </summary>
	public static class RelationsProviderTools
	{

		#region Inner Classes and Types

		internal static class ColumnName
		{

			public static class AttributeAssociation
			{
				public const string StartDate = "StartDate";
				public const string FinishDate = "FinishDate";
			}

			public static class AttributeDisassociation
			{
				public const string StartDate = "StartDate";
				public const string FinishDate = "FinishDate";
			}

		}

		#endregion // Inner Classes and Types

		#region Static Methods

		/// <summary>
		/// Reads association fields from the specified record into the specified association.
		/// </summary>
		/// <param name="record">The <see cref="IDataRecord"/> to read the fields from.</param>
        /// <param name="associatedItemIdField">The name of the field to read the associated item
        /// identifier from.</param>
		/// <param name="association">The <see cref="AssociationBase"/> to read the fields
		/// into.</param>
		public static void ReadAssociation(IDataRecord record, string associatedItemIdField, AssociationBase association)
		{
			Safety.AssertArgumentNotNull("record", record);
            Safety.AssertArgumentNotNull("associatedItemIdField", associatedItemIdField);
			Safety.AssertArgumentNotNull("association", association);

            association.AssociatedItemId = (Guid)record[associatedItemIdField];
			association.StartDate = (DateTime)record[ColumnName.AttributeAssociation.StartDate];
			association.FinishDate = record[ColumnName.AttributeAssociation.FinishDate] as DateTime?;
		}

		/// <summary>
		/// Reads disassociation fields from the specified record into the specified disassociation.
		/// </summary>
		/// <param name="record">The <see cref="IDataRecord"/> to read the fields from.</param>
        /// <param name="disassociatedItemIdField">The name of the field to read the 
        /// disassociated item identifier from.</param>
		/// <param name="disassociation">The <see cref="DisassociationBase"/> to read the 
		/// fields into.</param>
		public static void ReadDisassociation(IDataRecord record, string disassociatedItemIdField, DisassociationBase disassociation)
		{
			Safety.AssertArgumentNotNull("record", record);
            Safety.AssertArgumentNotNull("disassociatedItemIdField", disassociatedItemIdField);
            Safety.AssertArgumentNotNull("disassociation", disassociation);

            disassociation.DisassociatedItemId = (Guid)record[disassociatedItemIdField];
			disassociation.StartDate = (DateTime)record[ColumnName.AttributeDisassociation.StartDate];
			disassociation.FinishDate = record[ColumnName.AttributeDisassociation.FinishDate] as DateTime?;
		}

		#endregion // Static Methods

	}

}
